package com.kotlin.myapplication.mvp.ui.main.adapter

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.kotlin.myapplication.R
import com.kotlin.myapplication.bean.Weather
import com.kotlin.myapplication.bean.WeatherResult
import com.kotlin.myapplication.bean.realtime
import com.kotlin.myapplication.mvp.widget.SideslipListView
import com.kotlin.myapplication.utils.Logger
import kotlinx.android.synthetic.main.item_device_detail.view.*
import org.jetbrains.anko.imageResource

/**
 *@lil
 *@创建时间 2019/11/7 15:36
 *@描述
 */
class SideslipListAdapter(view: SideslipListView, context: Context, list: List<Weather<WeatherResult<realtime>>>) : BaseAdapter() {

    var sideslipListView: SideslipListView? = null
    var activity: Activity? = null
    var context: Context? = null
    var list: List<Weather<WeatherResult<realtime>>>? = null

    init {
        this.sideslipListView = view
        this.context = context
        if (null == list)
            this.list = ArrayList()
        else
            this.list = list

    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var view: View? = null
        var viewholder: SideslipViewHolder? = null
        if (position > list!!.count()) {
            return null
        }
        if (null == convertView) {
            view = View.inflate(context, R.layout.item_device_detail, null)
            viewholder = SideslipViewHolder(view)
            view.tag = viewholder
        } else {
            view = convertView
            viewholder = view.tag as SideslipViewHolder
        }
        list?.let {
            viewholder.view.name.text = it[position].result.city ?: ""
            viewholder.view.status.text = it[position].result.realtime.info ?: ""
            var direct = it[position].result.realtime.direct
            var power = it[position].result.realtime.power
            Logger.log("direct : " + direct)
            if (!direct.isEmpty())
                viewholder.view.co_infotext.text = direct + power ?: ""
            viewholder.view.voc_infotext.text = it[position].result.realtime.temperature + "℃" ?: ""
            context?.let {
                viewholder.view.header_img.imageResource = R.mipmap.weather
            }

        }


        return view
    }

    override fun getItem(position: Int): Any = list!!.get(position)


    override fun getItemId(position: Int): Long = position.toLong()


    override fun getCount(): Int = list?.count() ?: 0


    class SideslipViewHolder(var view: View)

}